//
//  BaseTableViewController.swift
//  ANOA
//
//  Created by zwb on 2018/6/15.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit
import MJRefresh
enum TableSearchType {
    case dayR,monthR,comS,hiddenx
}
class BaseTableViewController: UIViewController,DateWidgetable,UITableViewDelegate,UITableViewDataSource {
    // MARK: - Variables
    var page = 1
    var size = KSGlobal.pageSize
    
    var startDate = Date.todayPres(90)
    var endDate = Date.today
    
    // MARK: - Getter And Setter
    var startMonth = Date.preMonths(6)
    var endMonth = Date.currentMonth
    
    var t_begin_dt:TemplateModel = {
        var t = TemplateModel(JSON:["":""])!
        t.isFormSHow = "1"
        t.isFilterShow = "1"
        t.field_nm = "begin_dt"
        t.field_annotation = "开始日期"
        t.date_default_value = "5"
        t.field_type = TemplateType.date.rawValue
        return t
    }()
    var t_end_dt:TemplateModel = {
        var t = TemplateModel(JSON:["":""])!
        t.isFormSHow = "1"
        t.isFilterShow = "1"
        t.field_nm = "end_dt"
        t.field_annotation = "结束日期"
        t.date_default_value = "1"
        t.field_type = TemplateType.date.rawValue
        return t
    }()
    
 
    
    lazy var tableView: UITableView = {
        let v = UITableView()
        v.delegate = self
        v.dataSource = self
        return v
    }()
    lazy var dayRangeBanner:SearchDayRangeV = {
        let v = SearchDayRangeV(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: 50))
        return v
    }()
    lazy var monthRangeBanner:SearchMonthRangeV = {
        let v = SearchMonthRangeV(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: 50))
        return v
    }()
 
    lazy var comSearchBanner:ComSearch2V = {
        let v = ComSearch2V(frame: CGRect(x:0,y:0,width: KSGlobal.screenW,height: 50))
        return v
    }()
  
     override func viewDidLoad() {
        super.viewDidLoad()
        if #available(iOS 15.0, *) {
                    tableView.sectionHeaderTopPadding = 0
                }
        configureTableView()
        tableView.mj_header?.beginRefreshing()
    }
    
    func getSearchType() -> TableSearchType {
        return .comS
    }
        
    // MARK: Custom Functions
    func configureTableView() {
        var tabTopMargin = 50
        if getSearchType() == .dayR {
            view.addSubview(dayRangeBanner)
            dayRangeBanner.startSearchClo = { sarchStr,starD,endD in
                self.tableView.mj_header?.beginRefreshing()
            }
        } else if getSearchType() == .monthR {
            view.addSubview(monthRangeBanner)
            monthRangeBanner.startSearchClo = { sarchStr,starD,endD in
                self.tableView.mj_header?.beginRefreshing()
            }
        } else if getSearchType() == .hiddenx {
            tabTopMargin = 0
        } else {
            view.addSubview(comSearchBanner)
        }
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(tabTopMargin)
            make.right.left.bottom.equalToSuperview()

        }
        
        tableView.register(GlobalTableViewCell.self, forCellReuseIdentifier: GlobalTableViewCell.identifier)
        tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 150, right: 0)
        tableView.separatorStyle = .none
        tableView.estimatedRowHeight = 180
        tableView.rowHeight = UITableView.automaticDimension
        tableView.backgroundColor = Theme.BACKGROUND_COLOR
        tableView.mj_header = KSRefreshHeader { [unowned self] in
            page = 1
            self.network()
        }
        tableView.mj_footer = KSRefreshFooter { [unowned self] in
            page += 1
            self.network()
        }
        
        view.backgroundColor = Theme.BACKGROUND_COLOR
       
       
    }
    
    // 顶部刷新
    @objc func network() {
       
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell(frame: CGRect.zero)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return false
    }
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .none
    }
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override var shouldAutorotate: Bool {
        return true
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return UIInterfaceOrientation.portrait
    }
    
 
}
